/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import pojo.Thanhpho;
import until.HibernateUtil;

/**
 *
 * @author Admin
 */
public class thanhPhoDAO {

    //<editor-fold defaultstate="collapsed" desc="1.Hiển Thị Thành Phố">
    public static List<Thanhpho> layDanhSachThanhPho() {
        List<Thanhpho> ds = null;
        Session sc = HibernateUtil.getSessionFactory().openSession();
        try {
            String sql = "from Thanhpho";
            Query q = sc.createQuery(sql);
            ds = q.list();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            sc.close();
        }
        return ds;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="2.Hiển Thị Thành Phố Theo Mã">
    public static List<Thanhpho> layThanhPhoTheoID(String  mathanhpho){
        List<Thanhpho> tp = null;
        Session sc=HibernateUtil.getSessionFactory().openSession();
        
        try {
            
            String hql="from Thanhpho where ID ="+ mathanhpho+"";
            Query query=sc.createQuery(hql);
            tp = query.list();
            
        } catch (Exception e) {
            System.err.println(e);
            
        }finally{
            sc.close();
        }
        return tp;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="3.Thêm Thành Phố">
    public static boolean themThanhPho(Thanhpho tp) {
        boolean kq = true;
        Transaction transaction = null;
        Session sc = HibernateUtil.getSessionFactory().openSession();
        try {
            transaction = sc.beginTransaction();
            sc.update(tp);
            transaction.commit();
        } catch (Exception e) {
            System.err.println(e);
            transaction.rollback();
            kq = false;
        } finally {
            sc.close();
        }
        return kq;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="4.Cập Nhật Thành Phố">
    public static boolean capNhatThanhPho(Thanhpho tp) {
        boolean kq = true;
        Transaction transaction = null;
        Session sc = HibernateUtil.getSessionFactory().openSession();
        try {
            transaction = sc.beginTransaction();
            sc.save(tp);
            transaction.commit();
        } catch (Exception e) {
            System.err.println(e);
            transaction.rollback();
            kq = false;
        } finally {
            sc.close();
        }
        return kq;
    }
    //</editor-fold>
}
